﻿using System;

namespace Demo
{

    /**
     * 装饰（Decorator）模式：动态地给一个对象增加一些额外的职责，就增加对象功能来说，装饰模式远比生成子类实现更加灵活。装饰模式是一种对象结构型模式　
     * 
     * ***/

    class Program
    {
        static void Main(string[] args)
        {

            Component component = new Window();
            // 一次装饰
            Component componentSB = new ScrollBarDecorator(component);
            componentSB.Display();

            Console.WriteLine();
            // 二次装饰
            Component componentBB = new BlackBorderDecorator(componentSB);
            componentBB.Display();


        }






    }

    public abstract class Component
    {
        public abstract void Display();
    }

    /// <summary>
    /// 窗体类：具体构件类
    /// </summary>
    public class Window : Component
    {
        public override void Display()
        {
            Console.WriteLine("显示窗体!");
        }
    }

    /// <summary>
    /// 文本框类：具体构件类
    /// </summary>
    public class TextBox : Component
    {
        public override void Display()
        {
            Console.WriteLine("显示文本框!");
        }
    }

    /// <summary>
    /// 列表框类：具体构件类
    /// </summary>
    public class ListBox : Component
    {
        public override void Display()
        {
            Console.WriteLine("显示列表框!");
        }
    }

    /// <summary>
    /// 构件装饰类：抽象装饰类
    /// </summary>
    public class ComponentDecorator : Component
    {
        private Component component;

        public ComponentDecorator(Component component)
        {
            this.component = component;
        }

        public override void Display()
        {
            component.Display();
        }
    }

    /// <summary>
    /// 滚动条装饰类：具体装饰类
    /// </summary>
    public class ScrollBarDecorator : ComponentDecorator
    {
        public ScrollBarDecorator(Component component) : base(component)
        {

        }

        public override void Display()
        {
            this.SetScrollBar();
            base.Display();
        }

        public void SetScrollBar()
        {
            Console.WriteLine("为构件增加滚动条!");
        }
    }

    /// <summary>
    /// 黑色边框装饰类：具体装饰类
    /// </summary>
    public class BlackBorderDecorator : ComponentDecorator
    {
        public BlackBorderDecorator(Component component) : base(component)
        {

        }

        public override void Display()
        {
            this.SetScrollBar();
            base.Display();
        }

        public void SetScrollBar()
        {
            Console.WriteLine("为构件增加黑色边框!");
        }
    }


}
